<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class TopCategoryTeamsWidget extends Widget
{

    /**
     *
     * @param $data = array('expire' => 30, 'limit'=> 10, 'cid' => 1);
     */
    public function render($data)
    {
        $expire = !empty($data['expire']) ? intval($data['expire'] * 60) : 60 * 60;
        $limit  = !empty($data['limit']) ? intval($data['limit']) : 5;
        $cateID = !empty($data['cid']) ? intval($data['cid']) : 0;

        $name = 'cache_top_category_teams_'.$cateID;
        $teams = S($name);
        
        if (empty($teams))
        {
            $Team  = D('Team');
	        $map = array('status' => 1);
	        $order = array('best_answer_count' => 'desc');
	        
        	if ($cateID)
        	{
        		$cateIDs = get_sub_category_ids($cateID, 1);
        		$map['category_id'] = array('in', $cateIDs);
        	}
        	$teams = $Team->where($map)->order($order)->limit($limit)->select();
        	S($name, $teams, $expire);
        }
        
        $data['teams'] = $teams;

        $tplFile = TEMPLATE_PATH .'/Home/Widget_topCategoryTeams.html';
        $content = $this->renderFile($tplFile, $data);
        return $content;
    }

}

/* End of file TopCategoryTeamsWidget.class.php */
/* Location: ./App/Lib/Widget/TopCategoryTeamsWidget.class.php */
